﻿Imports DTO
Imports System.Data.SqlClient

Public Class HangHoaDAO
    Public Shared Function LoadBangHangHoa() As DataTable
        Return CKetNoi.LoadBang("select MaHangHoa, TenHangHoa from HangHoa ")
    End Function

    Shared Function layDSHangHoaBanHang() As List(Of HangHoaBanHangDTO)
        Dim dsHH As New List(Of HangHoaBanHangDTO)
        Dim cn As SqlConnection = CKetNoi.Connect
        Dim cmd As New SqlCommand
        cmd.Connection = cn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "usp_layDSHangHoaCuaBanHang"
        cn.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader
        While reader.Read
            Dim hh As New HangHoaBanHangDTO
            hh.MaHangHoa = reader("MaHangHoa")
            hh.TenHangHoa = reader("TenHangHoa")
            hh.SoLuongTon = reader("SoLuongTon")
            hh.TenKhoHang = reader("TenKhoHang")
            hh.MaKhoHang = reader("MaKhoHang")
            hh.GiaMua = reader("GiaMua")
            hh.GiaBanLe = reader("GiaBanLe")
            hh.GiaBanSi = reader("GiaBanSi")
            hh.TenDonVi = reader("TenDonVi")
            dsHH.Add(hh)
        End While
        cn.Close()
        Return dsHH
    End Function


    Public Shared Function LayDSLoaiHHCbx() As DataTable
        Return CKetNoi.GetData("select Ma, TenLoaiHangHoa from LoaiHangHoa ")
    End Function


    Public Shared Function LayDSHangHoa() As DataTable
        Return CKetNoi.GetData("LayDSHangHoa")
    End Function

    Public Shared Function ThemHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@tenHH", hh.TenHangHoa))
        lst.Add(New SqlParameter("@cc", hh.MaNhaCungCap))
        lst.Add(New SqlParameter("@nh", hh.MaNhomHang))
        lst.Add(New SqlParameter("@lhh", hh.MaLoaiHangHoa))
        lst.Add(New SqlParameter("@dv", hh.MaDonVi))
        lst.Add(New SqlParameter("@kh", hh.MaKhoHang))
        lst.Add(New SqlParameter("@xx", hh.XuatXu))
        lst.Add(New SqlParameter("@anh", hh.DiaChiAnh))
        lst.Add(New SqlParameter("@tktt", hh.TonKhoToiThieu))
        lst.Add(New SqlParameter("@tkht", hh.TonKhoHienTai))
        lst.Add(New SqlParameter("@gm", hh.GiaMua))
        lst.Add(New SqlParameter("@gbl", hh.GiaBanLe))
        lst.Add(New SqlParameter("@gbs", hh.GiaBanSi))
        lst.Add(New SqlParameter("@matt", hh.MaTrangThai))
        kt = CKetNoi.ExecQueryStoreProc("ThemHangHoa", lst)
        Return kt
    End Function

    Public Shared Function CapNhatHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@mahh", hh.MaHangHoa))
        lst.Add(New SqlParameter("@tenHH", hh.TenHangHoa))
        lst.Add(New SqlParameter("@cc", hh.MaNhaCungCap))
        lst.Add(New SqlParameter("@nh", hh.MaNhomHang))
        lst.Add(New SqlParameter("@lhh", hh.MaLoaiHangHoa))
        lst.Add(New SqlParameter("@dv", hh.MaDonVi))
        lst.Add(New SqlParameter("@kh", hh.MaKhoHang))
        lst.Add(New SqlParameter("@xx", hh.XuatXu))
        lst.Add(New SqlParameter("@anh", hh.DiaChiAnh))
        lst.Add(New SqlParameter("@tktt", hh.TonKhoToiThieu))
        lst.Add(New SqlParameter("@tkht", hh.TonKhoHienTai))
        lst.Add(New SqlParameter("@gm", hh.GiaMua))
        lst.Add(New SqlParameter("@gbl", hh.GiaBanLe))
        lst.Add(New SqlParameter("@gbs", hh.GiaBanSi))
        lst.Add(New SqlParameter("@matt", hh.MaTrangThai))
        kt = CKetNoi.ExecQueryStoreProc("CapNhatHangHoa", lst)
        Return kt
    End Function


    Public Shared Function XoaHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@mahh", hh.MaHangHoa))
        kt = CKetNoi.ExecQueryStoreProc("XoaHangHoa", lst)
        Return kt
    End Function


End Class
